package com.wcgksoft.controller.system;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.system.*;
import com.wcgksoft.service.SysRoleCodeService;
import com.wcgksoft.service.openAccount.OpenAccountService;
import com.wcgksoft.service.system.EnterpriseService;
import com.wcgksoft.service.system.GovernmentService;
import com.wcgksoft.service.system.SysRoleService;
import com.wcgksoft.service.system.SystemVersionService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 阿良
 * @version 1.0.0
 * @ClassName EnterpriseController.java
 * @Description 开设政府控制器
 * @createTime 2020年03月11日 10:22:00
 */
@Controller
@RequestMapping("government")
public class GovernmentController {

  @Autowired
  private EnterpriseService enterpriseService;

  @Autowired
  private SystemVersionService systemVersionService;
  @Autowired
  private OpenAccountService openAccountService;
  @Autowired
  private SysRoleService sysRoleService;
  @Autowired
  private SysRoleCodeService sysRoleCodeService;
  @Autowired
  private GovernmentService governmentService;

  /**
   * @throws
   * @title TODO
   * @description 政府列表查询
   * @author 阿良
   * @param: page
   * @param: limit
   * @updateTime 2020/3/6 15:23
   * @return: com.wcgksoft.common.util.Layui
   */
  @ResponseBody
  @RequestMapping(value = "list")
  public Layui findAll(
          @RequestParam(value = "page", required = false) Integer page,
          @RequestParam(value = "limit", required = false) Integer limit,
          @RequestParam(value = "term", required = false, defaultValue = "") String search_term) {

    DetachedCriteria criteria = DetachedCriteria.forClass(Government.class);
    if (!search_term.isEmpty()) {
      criteria.add(Restrictions.eq("govName", search_term));
    }
    List<Government> list = governmentService.findByConditions(criteria, page, limit);
    return Layui.data(0, "查询成功", governmentService.getTotalCount(), list);

  }


  /**
   * @throws
   * @title TODO
   * @description 添加修改政府
   * @author 阿良
   * @param: request
   * @param: data
   * @param: meun
   * @updateTime 2020/3/11 10:31
   * @return: com.wcgksoft.common.util.Layui
   */
  @ResponseBody
  @RequestMapping(value = "update")
  @Transactional
  public Layui update(HttpServletRequest request,
                      @RequestParam(value = "data", required = false) String data,
                      @RequestParam(value = "det", required = false) Integer[] det) throws Exception {
    JSONObject param = JSONObject.fromObject(data);
    if (param == null) {
      return Layui.data(1, "请求格式有误", 0, null);
    } else {
      Government government = (Government) JSONObject.toBean(JSONObject.fromObject(data), Government.class);
      DetachedCriteria criteria = DetachedCriteria.forClass(Enterprise.class);
      criteria.add(Restrictions.in("id", det));
      List<Enterprise> conditions = enterpriseService.findByConditions(criteria);
      government.setEnterprise(conditions);
      DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
      Date date = fmt.parse(param.getString("CreateDate"));
      government.setCreateDate(date);
      governmentService.saveUpdate(government);
      if (null == param.get("id") || "".equals(param.get("id"))) {
        SysUser user = new SysUser();
        user.setPhone(param.getString("loginaccount"));
        user.setPassWord(param.getString("loginpsd"));
        user.setCreateDate(new Date());
        user.setName(government.getGovName());
        user.setStatus(1);
        user.setGov_id(government.getId());
        SysRole role = new SysRole();
        role.setSysRoleCode(sysRoleCodeService.findById(5));
        role.setAdd_time(new Date());
        role.setSysRolename("政府角色");
        sysRoleService.save(role);
        user.setSysRole(role);/*TODO 企业ID是二目前写死*/
        openAccountService.save(user);
      }
      return Layui.data(0, "保存成功", 1, Layui.addList(government));
    }
  }

  /**
   * 删除政府
   *
   * @param id
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "delete")
  @Transactional
  public Layui delete(@RequestParam(value = "id") Integer id) {
    Government byId = governmentService.findById(id);
    governmentService.deleteCass(byId);

    return Layui.data(1, "删除成功", 1, null);
  }

  /**
   * 查询所有企业
   */
  @ResponseBody
  @RequestMapping(value = "findallent")
  @Transactional
  public Layui findallent() {
    List<Enterprise> list = enterpriseService.findAll(Enterprise.class);
    return Layui.data(0, "查询成功", enterpriseService.getTotalCount(), list);
  }
  /**
   * 选择切换企业
   */
  @ResponseBody
  @RequestMapping(value = "switcent")
  public Layui switcent(@RequestParam(value = "id", required = false) Integer id, HttpServletRequest req) {
    Enterprise id1 = enterpriseService.findById(id);
    req.getSession().setAttribute("goventerprise", id1);
    return Layui.data(0, "切换企业成功", 1, null);
  }
}
